﻿using ALMDDD.Domain.Interfaces.Repository;
using ALMDDD.Domain.Model;
using ALMDDD.Infrastructure.Context;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ALMDDD.Infrastructure.Repository
{
    public class SysUserRepository : Repository<Sys_User>, ISysUserRepository
    {
        public SysUserRepository(ALMContext almContext) : base(almContext)
        {
            
        }

        public Sys_User GetUserRoles(Guid id)
        {


            IQueryable<Sys_User> query = Query;
            string sf = query.ToString();

            IQueryable<Sys_User> userDb = _db.Set<Sys_User>();
            IQueryable<Sys_Role> RoleDB = _db.Set<Sys_Role>();
            IQueryable<SysUserRole> userRole = _db.Set<SysUserRole>();
            //Sys_Role retret = new Sys_Role();
            //var user1 = (from p in query where p.Id == id select p).ToString();

            var user = query.Where(x => x.Id == id).FirstOrDefault();
            //  var result1 = userRole.Where(x => x.UserId == id).ToString();
            //  //var sss = RoleDB.Where(x => x.Id==result1.RoleId).FirstOrDefault();

            var result = userRole.Where(x => x.UserId == id)
                .GroupJoin(RoleDB, a => a.RoleId, b => b.Id, (a, b) => b).FirstOrDefault();

            //var ss=  userDb.Include( x => x.Sys_Role.Select(u => u.Id == Guid.Parse("17592F85-9F89-4A40-874E-462B0904F3E8"))).FirstOrDefault();

            //  //var roleLists = null;
            List<Sys_Role> roleLists = new List<Sys_Role>() ;
            if (result != null)
            {
                roleLists = result.ToList();
            }
            return new Sys_User(user.Id, user.UserId, user.UserName, user.Gender, user.PassWord, user.Email, user.Phone, user.TrxTime, roleLists);


        }
    }
}
